/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package matematikapirma.sys;

import java.util.ArrayList;
import java.util.List;
import matematikapirma.Bounds;
import matematikapirma.Curve;
import matematikapirma.CurveAlgorithm;
import matematikapirma.Point;
import matematikapirma.Surface;

/**
 *
 * @author Tadas
 */
public class SimpleCurve implements Curve {

    private List<Point> initialPoints = new ArrayList<Point>();
    private List<Point> points = new ArrayList<Point>();
    private boolean open = Surface.LINES_OPEN;
    private Surface surface;
    double minx = 1e10, maxx = 0.0, miny = 1e10, maxy = 0.0;
    private CurveAlgorithm algorithm;
    private Bounds bounds;

    public SimpleCurve(Surface surface, CurveAlgorithm algorithm) {
        this.surface = surface;
        this.algorithm = algorithm;
    }

    public List<Point> getInitialPoints() {
        return initialPoints;
    }

    public List<Point> getPoints() {
        return points;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
        updateCurve(null);
    }

    public void addPoint(Point point) {
        initialPoints.add(point);
        updateCurve(point);
    }

    public void removePoint(Point point) {
        initialPoints.remove(point);
        updateCurve(point);
    }

    public void setCurveAlgorithm(CurveAlgorithm algorithm) {
        this.algorithm = algorithm;
        updateCurve(null);
    }

    public void addPoint(double x, double y) {
        Point point = new MPoint(x, y);
        this.initialPoints.add(point);
        updateCurve(point);
    }

    public Surface getSurface() {
        return surface;
    }


    public void updateCurve(Point point){
        points = algorithm.calculatePoints(initialPoints, open, getSurface().getSteps(), 0, 0);
        updateMinMax();
    }

    protected void updateMinMax(){
        minx = maxx = miny = maxy = 0.0;
        minx = miny = 1e10;
        for(Point point : points){
            if(maxx < point.getX()){
                maxx = point.getX();
            }
            if(minx > point.getX()){
                minx = point.getX();
            }
            if(maxy < point.getY()){
                maxy = point.getY();
            }
            if(miny > point.getY()){
                miny = point.getY();
            }
        }
        bounds = new SimpleBounds(minx, miny, maxx, maxy);
    }

    public Bounds getBounds() {
        return bounds;
    }

    public CurveAlgorithm getCurveAlgorithm() {
        return algorithm;
    }
}
